package fr.umlv.ig.MetroView;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import fr.umlv.ig.model.MetroModelImpl;
import fr.umlv.ig.xml.MetroModelFactory;

public class MetroApplication {

	private final JFrame frame = new JFrame("Jack BeauHeur");

	private static JPanel northPanel() {
		JPanel northPanel = new JPanel();
		JLabel title = new JLabel("Jacques Beauheur");
		title.setPreferredSize(new Dimension(1200, 50));
		northPanel.add(title);
		return northPanel;
	}

	private static JPanel centerPanel() {
		JPanel centerPanel = new JPanel(new FlowLayout());	
		centerPanel.setBackground(Color.YELLOW);
		centerPanel.setPreferredSize(new Dimension(100,700));

		MetroModelFactory metroFactory = null;
		try {
			metroFactory = MetroModelFactory.newInstance();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
		
		MetroModelImpl model = metroFactory.getMetroModelImpl();
		BufferedImage imageMap = new BufferedImage(906, 623, BufferedImage.TYPE_3BYTE_BGR);
		MetroMapStations metroMap = new MetroMapStations(model, imageMap);
		metroMap.drawMap();
		metroMap.setOpaque(true);
		metroMap.setPreferredSize(new Dimension(906,623));
		metroMap.setVisible(true);
		centerPanel.add(metroMap);
		
		return centerPanel;
	}


	private static JPanel southPanel() {
		JPanel southPanel = new JPanel();
		ImageIcon imageSouth = new ImageIcon(MetroApplication.class.getResource("../images/m1.jpg"));
		JLabel imageLabel2 = new JLabel(imageSouth);
		imageLabel2.setPreferredSize(new Dimension(1200, 100));
		southPanel.add(imageLabel2);
		return southPanel;
	}

	/**
	 * Initializes the application with a frame. 
	 */
	public MetroApplication()  throws IOException {
		frame.setSize(1300,800);
		frame.setPreferredSize(new Dimension(1300,700));
		frame.setMinimumSize(new Dimension(1300,700));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel mainPanel = new JPanel(new BorderLayout());
		frame.setContentPane(mainPanel);

		
		mainPanel.add(northPanel(), BorderLayout.NORTH);
		mainPanel.add(centerPanel(), BorderLayout.CENTER);
		mainPanel.add(southPanel(), BorderLayout.SOUTH);

	}


	/**
	 *Sets visible the windows with the game.
	 */
	public void start() {
		frame.pack();
		frame.setVisible(true);
	}
}
